package com.star.spring.ratelimit.config;

import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Configuration;

/**
 * 限流配置类
 * 
 * @author star
 * @since 2025-01-24
 */
@Data
@Configuration
@ConfigurationProperties(prefix = "rate-limit")
public class RateLimitConfig {

    /**
     * 本地限流配置
     */
    private Local local = new Local();

    /**
     * 分布式限流配置
     */
    private Distributed distributed = new Distributed();

    @Data
    public static class Local {
        /**
         * 令牌桶配置
         */
        private TokenBucket tokenBucket = new TokenBucket();

        /**
         * 滑动窗口配置
         */
        private SlidingWindow slidingWindow = new SlidingWindow();

        @Data
        public static class TokenBucket {
            /**
             * 桶容量
             */
            private int capacity = 100;

            /**
             * 每秒补充令牌数
             */
            private int refillRate = 10;
        }

        @Data
        public static class SlidingWindow {
            /**
             * 窗口大小（秒）
             */
            private int windowSize = 60;

            /**
             * 最大请求数
             */
            private int maxRequests = 100;
        }
    }

    @Data
    public static class Distributed {
        /**
         * Redis限流配置
         */
        private Redis redis = new Redis();

        @Data
        public static class Redis {
            /**
             * 固定窗口限流
             */
            private FixedWindow fixedWindow = new FixedWindow();

            /**
             * 滑动窗口限流
             */
            private SlidingWindow slidingWindow = new SlidingWindow();

            @Data
            public static class FixedWindow {
                /**
                 * 窗口大小（秒）
                 */
                private int windowSize = 60;

                /**
                 * 最大请求数
                 */
                private int maxRequests = 1000;
            }

            @Data
            public static class SlidingWindow {
                /**
                 * 窗口大小（秒）
                 */
                private int windowSize = 60;

                /**
                 * 最大请求数
                 */
                private int maxRequests = 1000;

                /**
                 * 精度（秒）
                 */
                private int precision = 1;
            }
        }
    }
}
